package com.haven.android.sdk.max;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import com.applovin.mediation.MaxAd;
import com.applovin.mediation.MaxError;
import com.applovin.mediation.MaxRewardedAdListener;
import com.applovin.mediation.ads.MaxRewardedAd;
import com.haven.android.sdk.Constant;
import com.haven.android.sdk.HavenAdSdk;


public class HavenMaxReward {
    private static HavenMaxReward adManager;
    private MaxRewardedAd maxRewardedAd;

    public static HavenMaxReward getInstance() {
        if (adManager == null) {
            adManager = new HavenMaxReward();
        }
        return adManager;
    }

    private MaxRewardedAd getRewardedAd(Context context,String adUnitId) {
        return MaxRewardedAd.getInstance(adUnitId, context);
    }

    public void loadAd(Context context) {
        maxRewardedAd =getRewardedAd(context, Constant.REWARD_PLACEMENTID);
        setListner(null);
        maxRewardedAd.loadAd();
    }

    private void setListner(HavenAdSdk.VideoRewardCallback listner) {
        maxRewardedAd.setListener(new MaxRewardedAdListener() {

            @Override
            public void onUserRewarded(MaxAd ad, com.applovin.mediation.MaxReward reward) {
//                Log.e("HavenW","onUserRewarded");
            }

            @Override
            public void onAdLoaded(MaxAd ad) {
                Log.d("HavenW","onMaxRewardLoaded");
            }

            @Override
            public void onAdDisplayed(MaxAd ad) {
                Log.e("HavenW","onAdDisplayed"+ad.getNetworkName());
            }

            @Override
            public void onAdHidden(MaxAd ad) {
                maxRewardedAd.loadAd();
                if (listner != null) {
                    listner.onAdOver();
                }
            }

            @Override
            public void onAdClicked(MaxAd ad) {
            }

            @Override
            public void onAdLoadFailed(String adUnitId, MaxError error) {
                Log.d("HavenW","onMaxRewardLoadFailed "+error.getCode()+ error.getMessage()+error.getMediatedNetworkErrorCode()+error.getMediatedNetworkErrorMessage());
            }

            @Override
            public void onAdDisplayFailed(MaxAd ad, MaxError error) {
                Log.e("HavenW","onAdDisplayFailed "+error.getCode() +error.getMessage()+error.getMediatedNetworkErrorCode()+error.getMediatedNetworkErrorMessage());
                if (listner != null) {
                    listner.onAdError();
                }
            }
        });
    }

    public void showVideo(Activity activity, HavenAdSdk.VideoRewardCallback listner) {
        if (maxRewardedAd == null) {
            maxRewardedAd =getRewardedAd(activity,Constant.REWARD_PLACEMENTID);
        }
        setListner(listner);
        if (maxRewardedAd.isReady()) {
            maxRewardedAd.showAd(activity);
        } else {
            if (listner!=null){
                listner.onAdError();
            }
            maxRewardedAd.loadAd();
        }
    }
}
